//
//  HomeCell.swift
//  Weibo项目
//
//  Created by vn on 16/7/23.
//  Copyright © 2016年 vn. All rights reserved.
//

import UIKit
import SnapKit
class HomeCell: UITableViewCell {
    
    
    //用来记录toolBar头部约束
    var toolBarTop:Constraint?
    
    
    
    var StatusModel:StatusViewModel?{
        
        didSet{

            //给原创微博的属性赋值
             Original.StatusModel = StatusModel
            
            
//                - 首先要先卸载底部视图的顶部约束
//                - 如果retweeted_status == nil 代表没有转发微博
//                    - 底部视图的top ==  原创微博的bottom
//                    - 隐藏转发微博
//                    - 反之 就是有转发微博
//                        - 给转发微博的ViewModel 赋值
//                            - 底部视图的top ==  转发微博的bottom
//                            - 显示转发微博
            
            //卸载约束,从新计算
            self.toolBarTop?.uninstall()
            
            if StatusModel?.homeModel?.retweeted_status == nil {
                //没有转发微博
                 //重新设置底部视图的约束
                 toolBar_v.snp_makeConstraints(closure: { (make) -> Void in
                    
                   self.toolBarTop = make.top.equalTo(Original.snp_bottom).constraint
                     
                })
                
                //隐藏转发微博
                self.retweeted.hidden = true
                
            }else{
                
                //有转发微博
                //给转发微博属性赋值
                 retweeted.ViewModel = StatusModel
                
                toolBar_v.snp_makeConstraints(closure: { (make) -> Void in
                    
                     self.toolBarTop = make.top.equalTo(retweeted.snp_bottom).constraint
                    
                })
                
               self.retweeted.hidden = false
                
            }
            
        }
        
     }
    
    
   override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style:style, reuseIdentifier: reuseIdentifier)
    
            setUpUI()
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //设置界面
    func setUpUI(){
        
        //添加View
        contentView.addSubview(Original)
        contentView.addSubview(retweeted)
        contentView.addSubview(toolBar_v)
        
        
        //设置约束
        Original.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.trailing.equalTo(contentView)
           
        }
        
        //转发
        retweeted.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(Original.snp_bottom)
            make.leading.trailing.equalTo(contentView)
            
       
        }
        
        //设置约束
        toolBar_v.snp_makeConstraints { (make) -> Void in
        
        //保存头部约束,方便根据有没有转发微博来动态修改
       self.toolBarTop = make.top.equalTo(retweeted.snp_bottom).constraint
        
           make.leading.trailing.equalTo(contentView)
            make.height.equalTo(35)
            
        }
        
        
        //设置了预估行高,这里就要约束contentView
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(toolBar_v)
            
            
            
        }
        
        
    }
    
    //懒加载控件
    lazy var Original:OriginalView = OriginalView()
    
    //转发微博
    lazy var  retweeted:RetweetedView = RetweetedView()
    
    
    //底部View
    lazy var toolBar_v:ToolBarView = ToolBarView()
    
    
      
}
